package com.fh.fruit.controller.activity;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.entity.activity.Activity;
import com.fh.fruit.entity.activity.ActivityRuleType;
import com.fh.fruit.service.activity.ActivityManager;
import com.fh.fruit.service.activity.ActivityRuleTypeManager;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/activityRuleType")
public class ActivityRuleTypeController extends BaseController {
	String menuUrl = "activityRuleType/listActivityRuleType.do"; // 地址(权限用)

	@Resource(name = "activityRuleTypeService")
	private ActivityRuleTypeManager activityRuleTypeManager;
	
	@Resource(name = "activityService")
	private ActivityManager activityManager;
	
	/**活动标签列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listActivityRuleType")
	public ModelAndView listActivityRuleType(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(StringUtils.isNotEmpty(keywords)){
			pd.put("keywords", keywords.trim());
		}
		page.setPd(pd);
		page.setShowCount(5);
		List<ActivityRuleType> activityRuleTypeList = activityRuleTypeManager.getActivityRuleType(page);
		mv.setViewName("farm/activity/activityRuleType_list");
		mv.addObject("activityRuleTypeList", activityRuleTypeList);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**
	 * 删除活动规则分类
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delActivityRuleType")
	@ResponseBody
	public String delActivityRuleType(HttpServletRequest requset) {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "删除活动");  
		String id = requset.getParameter("id");
		try {
			int num = activityRuleTypeManager.deleteActivityRuleType(Integer.parseInt(id));
			if(num>0){
				return "success";
			}else{
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "exception";
		}
	}
	/**
	 * 去新增活动规则分类页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAddActivityRuleType")
	public ModelAndView goAddActivityRuleType(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		//获取活动列表
		List<Activity>	activityList = activityManager.getActivity(page);
		ModelAndView mv = this.getModelAndView();
		mv.addObject("activityList", activityList);
		mv.addObject("msg", "saveAddActivityRuleType");
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		mv.setViewName("farm/activity/activityRuleType_edit");
		return mv;
	}
	
	/**
	 * 新增活动规则分类
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveAddActivityRuleType")
	public ModelAndView saveAddActivityRuleType(HttpServletRequest requset) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "新增活动标签");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("createId", user.getUSER_ID()); // 创建人
		pd.put("createTime", DateUtil.getTime()); // 创建时间
		int num=activityRuleTypeManager.addActivityRuleType(pd);
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 去修改活动规则分类页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goEditActivityRuleType")
	public ModelAndView goEditActivityRuleType(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		//获取活动列表
		List<Activity>	activityList = activityManager.getActivity(page);
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String id=pd.getString("id"); 
		ActivityRuleType activityRuleType = activityRuleTypeManager.getActivityRuleTypeById(Integer.parseInt(id));
		mv.addObject("activityRuleType", activityRuleType);
		mv.addObject("activityList", activityList);
		mv.addObject("id", id);
		mv.addObject("msg", "editActivityRuleType");
		mv.setViewName("farm/activity/activityRuleType_edit");
		return mv;
	}
	/**
	 * 修改活动分类
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editActivityRuleType")
	public ModelAndView editActivityRuleType(HttpServletRequest requset) throws Exception {

		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "修改活动标签");
		ModelAndView mv = this.getModelAndView();
		PageData pd = null;
		try {
			pd = this.getPageData();
			String id=pd.getString("id"); 
			ActivityRuleType activityRuleType = activityRuleTypeManager.getActivityRuleTypeById(Integer.parseInt(id));
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("updateId", user.getUSER_ID()); // 修改人
			pd.put("updateTime", DateUtil.getTime()); // 修改时间
			pd.put("createId", activityRuleType.getCreateId()); // 创建人
			pd.put("createTime", activityRuleType.getCreateTime()); // 创建时间
			int num=activityRuleTypeManager.updateActivityRuleType(pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
			mv.addObject("msg", "failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
}
